//
//  UIImage+JACKCategory.m
//  jack_weibo_OC
//
//  Created by jack on 16/4/27.
//  Copyright © 2016年 jack. All rights reserved.
//

#import "UIImage+JACKCategory.h"

@implementation UIImage (JACKCategory)


//截图
+ (UIImage *)getScreenShot;
{
    //获取主window
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    CGRect rect = window.frame;
    
    //打开图形上下文
    UIGraphicsBeginImageContext(rect.size);
    
    //渲染图片 afterScreenUpdates:是否刷新后截图
    [window drawViewHierarchyInRect:rect afterScreenUpdates:YES];
    
    //从图形上下文中获取图片
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    return resultImage;
}

//缩放图片
- (UIImage *)scaleImage:(CGFloat) width;
{
    //根据width计算出heigh
    CGFloat height = self.size.height * width / self.size.width;
    
    CGRect rect = CGRectMake(0, 0, width, height);
    
    //开启图形上下文
    UIGraphicsBeginImageContext(rect.size);
    
    //渲染图片
    [self drawInRect:rect];
    
    //从图形上下文中获取图片
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    return resultImage;
}

//拉伸图片
- (UIImage *)stretchImage;
{
    return [self stretchableImageWithLeftCapWidth:self.size.width * 0.5 topCapHeight:self.size.height * 0.5];
}


@end
